package com.administrator.cloudapplication.activitys;

import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.adapter.WeatherAdapter;
import com.administrator.cloudapplication.base.BaseActivity;
import com.administrator.cloudapplication.callback.ReqCallBack;
import com.administrator.cloudapplication.constant.Urls;
import com.administrator.cloudapplication.models.BankModel;
import com.administrator.cloudapplication.models.WeatherModel;
import com.administrator.cloudapplication.tools.GsonUtils;
import com.administrator.cloudapplication.tools.HttpUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/4/26.
 */

public class QueryActivity extends BaseActivity implements ReqCallBack {
    int type;
    ImageView image_query;
    ListView lv_query;
    ArrayList<WeatherModel.ResultsBean.WeatherDataBean> mDataBean = new ArrayList<>();
    ArrayList<BankModel.PointListBean> mBankDataBean = new ArrayList<>();
    WeatherAdapter mWeatherAdapter;
    String keyWord, ak, url, location;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_query);
        image_query = (ImageView) findViewById(R.id.image_query);
        lv_query = (ListView) findViewById(R.id.lv_query);
        initTitleView();
        type = getIntent().getIntExtra("type", type);
        switch (type) {
            case 5:
                image_query.setImageResource(R.mipmap.ic_more_weather_bg);
                mWeatherAdapter = new WeatherAdapter(this, mDataBean, null, 0);
                lv_query.setAdapter(mWeatherAdapter);
                tvTitle.setText("天气查询");
                keyWord = "天气";
                ak = "GuZriL3rkm1MUnyTyfsNGvTC";
                url = Urls.WEATHER;
                location = "洛阳";
                break;
            case 4:
                image_query.setImageResource(R.mipmap.ic_more_hospital_bg);
                mWeatherAdapter = new WeatherAdapter(this, null, mBankDataBean, 1);
                lv_query.setAdapter(mWeatherAdapter);
                tvTitle.setText("医院查询");
                keyWord = "医院";
                ak = "BGwoWdijZ9NBZ4wXchXn1pUj";
                url = Urls.Nearby;
                location = "112.434801,34.669488";
                break;
            case 6:
                image_query.setImageResource(R.mipmap.ic_more_park_bg);
                mWeatherAdapter = new WeatherAdapter(this, null, mBankDataBean, 1);
                lv_query.setAdapter(mWeatherAdapter);
                tvTitle.setText("公园查询");
                keyWord = "公园";
                ak = "BGwoWdijZ9NBZ4wXchXn1pUj";
                url = Urls.Nearby;
                location = "112.434801,34.669488";
                break;
            case 7:
                image_query.setImageResource(R.mipmap.ic_more_bank_bg);
                mWeatherAdapter = new WeatherAdapter(this, null, mBankDataBean, 1);
                lv_query.setAdapter(mWeatherAdapter);
                tvTitle.setText("银行查询");
                keyWord = "银行";
                ak = "BGwoWdijZ9NBZ4wXchXn1pUj";
                url = Urls.Nearby;
                location = "112.434801,34.669488";
                break;
        }
    }

    @Override
    protected void initData() {
        requestWeb();
    }

    @Override
    protected void onLeftClick() {
        finish();
    }

    @Override
    protected void onRightClick() {

    }

    private void requestWeb() {
        showLoading();
        HashMap<String, String> param = new HashMap();
        param.put("location", location);
        param.put("output", "json");
        param.put("keyWord", keyWord);
        param.put("sort_rule", "0");
        param.put("radius", "5000");
        param.put("ak", ak);
        param.put("number", "20");
        HttpUtils.getInstance(this).requestGet("", url, param, this, 0);
    }

    @Override
    public void onReqSuccess(String result, int flag) {
        stopLoading();
        mDataBean.clear();
        mBankDataBean.clear();
        switch (type) {
            case 4:
            case 6:
            case 7:
                BankModel mBankModel = GsonUtils.json2Bean(result, BankModel.class);
                mBankDataBean.addAll(mBankModel.getPointList());
                break;
            case 5:
                WeatherModel mWeatherModel = GsonUtils.json2Bean(result, WeatherModel.class);
                mDataBean.addAll(mWeatherModel.getResults().get(0).getWeather_data());
                break;
        }
        mWeatherAdapter.notifyDataSetChanged();
    }

    @Override
    public void onReqFailed(String errorMsg, int flag) {
        Toast.makeText(this, "请检查您的网络", Toast.LENGTH_SHORT).show();
    }
}
